package com.leessy.r229.utils

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.telephony.PhoneStateListener
import android.telephony.SignalStrength
import android.telephony.TelephonyManager
import android.util.Log
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.PhoneUtils
import com.leessy.r229.App
import com.leessy.r229.config.Config
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

/**
 * @author : Created by liucheng. on 2021/04/12
 * @description : -
 */
class PhoneUtil {
    companion object {

        /*初始化网络信息，刚开机是不一定生效*/
        fun initIMEI() {
            if (ActivityCompat.checkSelfPermission(
                    App.app,
                    Manifest.permission.READ_PHONE_STATE
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return
            }
//            Log.d("TAG", "initIMEI: ${PhoneUtils.getPhoneStatus()}")
            PhoneUtils.getIMEI()?.takeIf { it.isNotEmpty() }?.let { Config.DEV_IMEI = it }


//            PhoneUtils.getIMSI()?.takeIf { it.isNotEmpty() }?.let { Config.DEV_IMSI = it }
            Config.DEV_IMSI = if (PhoneUtils.getIMSI().isNullOrEmpty()) "" else PhoneUtils.getIMSI()

            Config.DEV_isSimCardReady = PhoneUtils.isSimCardReady()
            val tm = App.app.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
            if (Config.DEV_isSimCardReady) {
                tm.simSerialNumber?.takeIf { it.isNotEmpty() }
                    ?.let { Config.DEV_ICCID = it }/*有SIM卡才获该值*/
                val deviceIccid = DevicesUtil.getDeviceIccid()
                if (deviceIccid.length > 8) Config.DEV_ICCID = deviceIccid
                Log.d("TAG", "initIMEI iccid 属性: $deviceIccid")
                DevicesUtil.getSystemVer()
                tm.listen(object : PhoneStateListener() {
                    @RequiresApi(Build.VERSION_CODES.M)
                    override fun onSignalStrengthsChanged(signalStrength: SignalStrength?) {
                        super.onSignalStrengthsChanged(signalStrength)
                        Config.signalIntensityFor4G = signalStrength?.level ?: 0
                        Log.d("TAG", "initIMEI 信号: ${Config.signalIntensityFor4G}")
//                        val asu = signalStrength!!.gsmSignalStrength
//                        val lastSignal = -113 + 2 * asu
//                        Log.d("TAG", "initIMEI 信号2: $lastSignal")
//                        if (lastSignal > 0) {
//                            val mobileNetworkSignal = lastSignal.toString() + "dBm"
//                            Log.d("TAG", "initIMEI onSignalStrengthsChanged: $mobileNetworkSignal")
//                        }
                    }
                }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
            }else{
                Config.DEV_ICCID = ""
            }
        }

        /*首次初始化，由于刚开机时无法检测到4G模块，所以需要延迟一段时间再检测*/
        fun firstInitIMEI() {
            Observable.intervalRange(0, 5, 0, 30, TimeUnit.SECONDS)
                .map {
                    initIMEI()
                    "${Config.DEV_IMEI}"
                }
                .filter { !it.isNullOrBlank() }
                .take(1)//拿到一次数据即可
                .subscribe({ Log.d("TAG", "firstInitIMEI: $it") }, {})
        }
    }
}